package com.zsrt.devbase.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.zsrt.devbase.dto.BaseDTO;

/**
 * Created by 奈何桥桥1 on 2017/1/8.
 */
public class DataDeleteZeroUtil {
    /**
     * 解决数据库查询创建时间与修改时间后缀有.0的问题
     * @param list
     * @param <T>
     * @return
     */
    public static <T extends BaseDTO> List<T> changeCreateTimeForList(List<T> list){
        for (T l:list){
            if("" != l.getCreateTime() && null != l.getCreateTime()){
                l.setCreateTime(changeTime(l.getCreateTime()));
            }
            if("" != l.getModifyTime() && null != l.getModifyTime()){
                l.setModifyTime(changeTime(l.getModifyTime()));
            }
        }
        return list;
    }

    /**
     * 解决时间后缀带.0的问题，返回正确格式的时间
     * @param time
     * @return
     */
    public static String changeTime(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = null;
        try {
            parse = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }


        String format = sdf.format(parse);
        return format;
    }
}
